//
//  FirstCollectionViewCell.m
//  HappinessTrip
//
//  Created by dllo on 15/10/18.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "FirstCollectionViewCell.h"
#import "FirstTableViewCell.h"
#import "GetDataMethods.h"
#import "FirstItems.h"
#import "Model.h"


@implementation FirstCollectionViewCell

-(void)dealloc
{
    [_tabelView release];
    [super dealloc];
}

-(void)changeColor{
    self.tabelView.backgroundColor = [Model shareInstance].BackgroundColor;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _tabelView = [[UITableView alloc]initWithFrame:self.contentView.frame style:UITableViewStylePlain];
        _tabelView.dataSource = self;
        _tabelView.delegate = self;
        [self changeColor];
        [self.contentView addSubview:_tabelView];
        [_tabelView release];
        
        //初始化加载中视图
        self.progressView = [[MBProgressHUD alloc]initWithView:self.contentView];
        _progressView.labelText = @"玩命加载中";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
        [self.contentView addSubview:_progressView];
        [_progressView release];
    }
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    return self;
}
- (void)setUrlString:(NSString *)urlString
{
    _urlString = [urlString retain];
    [self getData];


}

-(void)getData
{
    self.itemArray = [NSMutableArray array];
    [GetDataMethods getDataByString:_urlString BodyString:nil WithDataBlock:^(id data) {
        NSArray *array = [data objectForKey:@"items"];
        for (NSDictionary *dic in array) {
            FirstItems *item = [[FirstItems alloc]init];
            [item setValuesForKeysWithDictionary:dic];
            [_itemArray addObject:item];
        }
        [_tabelView reloadData];
        [_progressView removeFromSuperview];
        
    }];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[FirstTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_itemArray.count != 0) {
        cell.item = _itemArray[indexPath.row];
    }
    cell.backgroundColor = [Model shareInstance].BackgroundColor;
    cell.backImageView.backgroundColor = [UIColor colorWithRed:0.56 green:0.53 blue:0.93 alpha:1];
    cell.nameLabel.textColor = [Model shareInstance].TitleColor;
    cell.tipsLabel.textColor = [Model shareInstance].TitleColor;
    cell.ratingLabel.textColor = [Model shareInstance].TitleColor;
    cell.visitLabel.textColor = [Model shareInstance].TitleColor;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _itemArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
}
















@end
